package com.baiqian.doupo.hud;

import com.baiqian.doupo.Utils;
import com.baiqian.doupo.capability.DouPoModCapability;
import com.baiqian.doupo.capability.douqi.IDouQiCapability;
import com.baiqian.doupo.douji.DouJi;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

//将HUD加载到Mod总线初始化
@Mod.EventBusSubscriber(value = Dist.CLIENT)
class HudClientEvent {
    @SubscribeEvent
    public static void onOverlayRender(RenderGameOverlayEvent event) {
        //避免其他类型的渲染糊在一起
        if (event.getType() != RenderGameOverlayEvent.ElementType.ALL) {
            return;
        }
        //确定是显示给玩家的
        if (Minecraft.getInstance().player == null) {
            return;
        }
        MainHUD mainHUD = new MainHUD(event.getMatrixStack());
        mainHUD.render();
    }
}

public class MainHUD extends AbstractGui {
    private final int magin;//定义为距离左上角应有的外边距
    private final Minecraft minecraft;
    //载入四个HUD
    private final ResourceLocation HUDDouQiTop = new ResourceLocation(Utils.MOD_ID, "textures/gui/douqi_top.png");
    private ResourceLocation HUDDouQiSoulColor;
    private final ResourceLocation HUDDouQiColor1 = new ResourceLocation(Utils.MOD_ID, "textures/gui/color1.png");
    private final ResourceLocation HUDDouQiColor2 = new ResourceLocation(Utils.MOD_ID, "textures/gui/color2.png");
    private final ResourceLocation HUDDouQiSetPutColor = new ResourceLocation(Utils.MOD_ID, "textures/gui/douqi_set_put_color.png");
    private final ResourceLocation HUDDouQiSetPutTop = new ResourceLocation(Utils.MOD_ID, "textures/gui/douqi_set_put_top.png");
    private final ResourceLocation HUDDouQiDouJiRebirth = new ResourceLocation(Utils.MOD_ID, "textures/douji/douji_rebirth.png");
    private final ResourceLocation HUDDouQiDouJiUse = new ResourceLocation(Utils.MOD_ID, "textures/douji/douji_use.png");
    private final ResourceLocation[] HUDDouQiIcon;
    private final MatrixStack matrixStack;
    private int SoulMain = 0;
    private final int IconMax = 7;//斗技可用图标数量

    public MainHUD(MatrixStack matrixStack) {
        this.magin = 5;
        this.minecraft = Minecraft.getInstance();
        this.matrixStack = matrixStack;
        HUDDouQiSoulColor = new ResourceLocation(Utils.MOD_ID, "textures/gui/douqi_soul_color_0.png");
        HUDDouQiIcon = new ResourceLocation[IconMax];
        for(int i=0;i<IconMax;++i)
            HUDDouQiIcon[i]=new ResourceLocation(Utils.MOD_ID, "textures/douji/douji_"+i+".png");
    }

    public void render() {
        LazyOptional<IDouQiCapability> douqiCap = null;
        if (Minecraft.getInstance().player != null) {
            douqiCap = Minecraft.getInstance().player.getCapability(DouPoModCapability.DOUQI_CAPABILTTY);
            if (douqiCap.isPresent()) {
                douqiCap.ifPresent((l) -> {
                    if(l.getSoulPropertyMain()!=l.getSoulPropertyMain()) {
                        HUDDouQiSoulColor = new ResourceLocation(Utils.MOD_ID, "textures/gui/douqi_soul_color_" + l.getSoulPropertyMain() + ".png");
                    }
        //保证图片显示颜色正确
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);

        //逐个显示HUD图

        //显示灵魂力以及主面板
        if(SoulMain!=l.getSoulPropertyMain()) {
            SoulMain = l.getSoulPropertyMain();
            HUDDouQiSoulColor = new ResourceLocation(Utils.MOD_ID, "textures/gui/douqi_soul_color_"+SoulMain+".png");
        }
        this.minecraft.getTextureManager().bindTexture(HUDDouQiSoulColor);
        blit(matrixStack, magin / 2, (magin + 85 - (int) (85 * l.getSoulPercentage())) / 2, 0, (float) ((85 - (85 * l.getSoulPercentage()))/2), 85 / 2, (int) (85 * l.getSoulPercentage() / 2), 85 / 2, 85 / 2);
        this.minecraft.getTextureManager().bindTexture(HUDDouQiTop);
        blit(matrixStack, magin / 2, magin / 2, 0, 0, 300 / 2, 85 / 2, 300 / 2, 85 / 2);

        //显示斗气等级
        drawString(matrixStack, this.minecraft.fontRenderer, l.getDouQiLevelStr(), (magin + 90) / 2, (magin + 3) / 2, 0x000000);

        //显示斗气浓度
        TranslationTextComponent content = new TranslationTextComponent("gui." + Utils.MOD_ID + ".width", (double) ((long) (l.getDouQiCompress() * 100)) / 100);
        drawString(matrixStack, this.minecraft.fontRenderer, content, (magin + 90+5) / 2, (magin + 25 + 10) / 2, 0x000000);

        //显示斗气量
        this.minecraft.getTextureManager().bindTexture(HUDDouQiColor1);
        blit(matrixStack, (magin + 90 + 5) / 2, (magin + 25 + 3 + 25 + 5) / 2, 0, 0, (int) (178 * (l.getDouQiHaveMax()/l.getDouQiHaveMaxLevelUp())) / 2, (20) / 2, (178) / 2, (20) / 2);
        this.minecraft.getTextureManager().bindTexture(HUDDouQiColor2);
        blit(matrixStack, (magin + 90 + 5) / 2, (magin + 25 + 3 + 25 + 5) / 2, 0, 0, (int) (178 * (l.getDouQi(1)/l.getDouQiHaveMaxLevelUp())) / 2, (20) / 2, (178) / 2, (20) / 2);

        //显示斗技
        for(int i=0;i<4;++i) {
            if(l.getDouJiGroup()[l.getDouJiGroupChoose()*4+i]!=-1) {
                DouJi dou_ji = l.getDouJis().get(l.getDouJiGroup()[l.getDouJiGroupChoose() * 4 + i]);
                this.minecraft.getTextureManager().bindTexture(HUDDouQiIcon[dou_ji.douji_icon]);
                blit(matrixStack, (magin + 100+i*20) / 2, (magin + 96)/2, 0, 0, 16 / 2, 16 / 2, 16 / 2, 16 / 2);
                this.minecraft.getTextureManager().bindTexture(HUDDouQiDouJiUse);
                blit(matrixStack, (magin + 100+i*20) / 2, (magin + 96+(16-16*dou_ji.use_time/dou_ji.douji_keep_time))/2, 0, 0, 16 / 2,
                        (16*dou_ji.use_time/dou_ji.douji_keep_time) / 2,
                        16 / 2, 16 / 2);
                this.minecraft.getTextureManager().bindTexture(HUDDouQiDouJiRebirth);
                blit(matrixStack, (magin + 100+i*20) / 2, (magin + 96 +(16-16*dou_ji.rebirth_time/dou_ji.douji_use_rebirth_time))/2, 0, 0, 16 / 2,
                        (16*dou_ji.rebirth_time/dou_ji.douji_use_rebirth_time) / 2,
                        16 / 2, 16 / 2);
            }
        }

        if (l.getLevelBig() > 0) {
            //显示斗气输出百分比
            this.minecraft.getTextureManager().bindTexture(HUDDouQiSetPutColor);
            blit(matrixStack, (magin + 2) / 2, (int) ((magin + 85 + 10 + 2 + 196 - (196 * l.getDouQiReleasePercentage())) / 2), 0, 0, 8 / 2, (int) (196 * l.getDouQiReleasePercentage() / 2), 8 / 2, 196 / 2);
            this.minecraft.getTextureManager().bindTexture(HUDDouQiSetPutTop);
            blit(matrixStack, (magin) / 2, (magin + 85 + 10) / 2, 0, 0, 12 / 2, 200 / 2, 12 / 2, 200 / 2);
        }
                        }
                );
            }
        }
//        if (Minecraft.getInstance().player != null && douqiCap.isPresent()) {
//            douqiCap.ifPresent((l) -> {
//                        drawString(matrixStack, this.minecraft.fontRenderer, "拥有斗气:"+l.getDouQiHave(), (20) / 2, (90) / 2, 0xaa0000);
//                        drawString(matrixStack, this.minecraft.fontRenderer, "伤害:"+l.getDouQiDamge(), (20) / 2, (120) / 2, 0xaa0000);
//                        drawString(matrixStack, this.minecraft.fontRenderer, "防御:"+l.getDouQiDefense(), (20) / 2, (150) / 2, 0xaa0000);
//                        drawString(matrixStack, this.minecraft.fontRenderer, "输出率:"+l.getDouQiPut(), (20) / 2, (180) / 2, 0xaa0000);
//                        drawString(matrixStack, this.minecraft.fontRenderer, "灵魂:"+l.getSoul(), (20) / 2, (210) / 2, 0xaa0000);
//                        drawString(matrixStack, this.minecraft.fontRenderer, "灵魂上限:"+l.getSoulMax(), (20) / 2, (240) / 2, 0xaa0000);
//                        drawString(matrixStack, this.minecraft.fontRenderer, "总纯化:"+l.getWidthAll(), (20) / 2, (270) / 2, 0xaa0000);
//                        drawString(matrixStack, this.minecraft.fontRenderer, "总等级:"+l.getLevelAll(), (20) / 2, (300) / 2, 0xaa0000);
//                        drawString(matrixStack, this.minecraft.fontRenderer, "升级速率:"+l.getDouQiGrowSpeed(), (20) / 2, (330) / 2, 0xaa0000);
//                        drawString(matrixStack, this.minecraft.fontRenderer, "大等级:"+l.getDouQiLevelBig(), (20) / 2, (360) / 2, 0xaa0000);
//                        drawString(matrixStack, this.minecraft.fontRenderer, "中等级:"+l.getDouQiLevelMiddel(), (20) / 2, (390) / 2, 0xaa0000);
//                        drawString(matrixStack, this.minecraft.fontRenderer, "小等级:"+l.getDouQiLevelSmall(), (20) / 2, (420) / 2, 0xaa0000);
//                    }
//            );
//        }
    }
}
